package com.example.controller;

import com.example.entity.Article;
import com.example.entity.Result;
import com.example.entity.Cases;
import com.example.entity.Video;
import com.example.service.ArticleService;
import com.example.service.CasesService;
import com.example.service.VideoService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 对文章，视频以及案例进行管理
 */
@RestController
@RequestMapping("/resource")
public class ContentController {

    @Autowired
    private ArticleService articleService;
    @Autowired
    private CasesService casesService;
    @Autowired
    private VideoService videoService;

    //查询文章列表
    @GetMapping("/article")
    public Result<Map<String, Object>> getArticleByPage(
            @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
            @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize){
        Map<String, Object> data = articleService.getArticleByPage(pageNum, pageSize);
        return Result.success(data);
    }

    //搜索文章
    @GetMapping("/article/search")
    public Result<Map<String, Object>> searchArticlesByTitle(
            @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
            @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
            @RequestParam(value = "titlePart", required = false) String titlePart) {
        Map<String, Object> data = articleService.searchArticlesByTitle(pageNum, pageSize, titlePart);
        return Result.success(data);
    }

    //根据id展示文章具体内容
    @GetMapping("/article/{id}")
    public Result<Article> getArticleById(@PathVariable Integer id) {
        Article article = articleService.getArticleById(id);
        if (article != null) {
            return Result.success(article);
        } else {
            return Result.error("Article not found");
        }
    }

    //查询案例列表
    @GetMapping("/cases")
    public Result<Map<String, Object>> getCasesByPage(
            @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
            @RequestParam(value = "pageSize", defaultValue = "20") Integer pageSize) {
        Map<String, Object> data = casesService.getCasesByPage(pageNum, pageSize);
        return Result.success(data);
    }

    //搜索案例
    @GetMapping("/cases/search")
    public Result<Map<String, Object>> searchCases(
            @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
            @RequestParam(value = "pageSize", defaultValue = "20") Integer pageSize,
            @RequestParam(value = "titlePart", required = false) String titlePart,
            @RequestParam(value = "author", required = false) String author) {
        Map<String, Object> data = casesService.searchCases(pageNum, pageSize, titlePart, author);
        return Result.success(data);
    }

    //根据id展示实例具体内容
    @GetMapping("/cases/{id}")
    public Result<Cases> getCaseById(@PathVariable Integer id) {
        Cases caseDetail = casesService.getCaseById(id);
        if (caseDetail != null) {
            return Result.success(caseDetail);
        } else {
            return Result.error("Case not found");
        }
    }

    //查询视频列表
    @GetMapping("/videos")
    public Result<Map<String, Object>> getVideosByPage(
            @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
            @RequestParam(value = "pageSize", defaultValue = "20") Integer pageSize) {
        Map<String, Object> data = videoService.getVideosByPage(pageNum, pageSize);
        return Result.success(data);
    }

    //搜索视频
    @GetMapping("/videos/search")
    public Result<Map<String, Object>> searchVideos(
            @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
            @RequestParam(value = "pageSize", defaultValue = "20") Integer pageSize,
            @RequestParam(value = "titlePart", required = false) String titlePart) {
        Map<String, Object> data = videoService.searchVideos(pageNum, pageSize, titlePart);
        return Result.success(data);
    }

}
